<?php


namespace App\Service\client;


use App\Common\RedisUtils;
use App\Common\Res;
use App\Common\SendMsgUtils;
use App\Common\StringUtils;
use App\Constants\ErrorCode;
use App\Model\WeworkAccount;
use App\Model\WeworkFriend;
use App\Model\TaskMobile;
use App\Request\client\UserRequest;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class userService
{
    use StringUtils, Res, RedisUtils, SendMsgUtils;

    /**
     *获取用户信息
     * @param array $params
     * @return array
     */
    public function appUploadFriendInfo($fd, array $params): array
    {
        // ApplicationContext::getContainer()->get(UserRequest::class)->validated($params['data'], __FUNCTION__);
        $params_data = $params['data'];
        if (empty($params_data['mobile']) && empty($params_data['nickname'])) {
            return $this->Error(ErrorCode::ERROR, '好友手机号码和昵称不能同时为空');
        }
        Db::beginTransaction();
        try {
            /*根据token查询redis是否存在数据*/
            $token = trim($params['token']);
            $deviceInfo = $this->hGet($this->hKey, $token);
            if (empty($deviceInfo)) {
                return $this->Error(ErrorCode::ERROR, '设备登录信息错误');
            }
            $deviceArr = json_decode($deviceInfo, true);
            $account = WeworkAccount::where(['marketUserId' => $deviceArr['marketUserId']])->first();
            if (empty($account)) {
                return $this->Error(ErrorCode::ERROR, '账号绑定有误');
            }
            $params_data['account_id'] = $account['id'];
            $userArr = $this->getUserArr($params_data);
            if (!empty($userArr['mobile'])) {
                $result = WeworkFriend::updateOrCreate(['mobile' => $userArr['mobile'], 'account_id' => $account['id']], $userArr);
            } else {
                $result = WeworkFriend::updateOrCreate(['nickname' => $userArr['nickname'], 'account_id' => $account['id']], $userArr);
            }
            /*提交用户信息*/
            $data['bookname'] = $userArr['bookname'];
            $data['status'] = 1;
            $data['msg'] = '提交信息成功';
            $result = $this->sendAppUploadFriendInfo($fd, $data);
            Db::commit();
            return $this->Success('提交信息成功', $result);
        } catch (\Throwable $ex) {
            Db::rollBack();
            $data['status'] = 0;
            $data['msg'] = $ex->getMessage();
            $result = $this->sendAppUploadFriendInfo($fd, $data);
            return $this->Error(ErrorCode::ERROR, '提交信息失败' . $ex->getMessage());
        }
    }

    /**
     * 获取用户信息
     * @param $params
     * @return array
     */
    public function getUserArr($params): array
    {
        if (!empty($params['add_time']) && trim($params['add_time']) != 'null')
            $data['addTime'] = strtotime($params['add_time']);
        if (!empty($params['nickname']))
            $data['nickname'] = $params['nickname'];
        if (!empty($params['sex']) && trim($params['sex']) != 'null')
            $data['sex'] = $params['sex'];
        if (!empty($params['path']) && trim($params['path']) != 'null')
            $data['path'] = $params['path'];
        if (!empty($params['describe']) && trim($params['describe']) != 'null')
            $data['remark'] = $params['describe'];
        if (!empty($params['mobile']))
            $data['mobile'] = $params['mobile'];
        $data['account_id'] = $params['account_id'];
        $data['userId'] = $this->getRandomUserId();
        $data['bookname'] = $this->getRandomName();
        return $data;
    }

}